package com.mszlu.news.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.news.admin.mapper.ComplainMapper;
import com.mszlu.news.admin.model.params.ComplainParam;
import com.mszlu.news.admin.pojo.Complain;
import com.mszlu.news.admin.service.ComplainService;
import com.mszlu.news.admin.vo.ComplainVo;
import com.mszlu.news.admin.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ComplainServiceImpl implements ComplainService {
    @Autowired
    private ComplainMapper complainMapper;

    @Override
    public Result addComplain(ComplainParam complainParam) {
//        HashMap<String,Object> objectHashMap=new HashMap<>();
//        objectHashMap.put("article_id",complainParam.getArticleId());
//        objectHashMap.put("user_id",complainParam.getUserId());
//        complainMapper.selectByMap(objectHashMap);
        QueryWrapper<Complain> complainQueryWrapper=new QueryWrapper<>();
        complainQueryWrapper.eq("article_id",complainParam.getArticleId());
        complainQueryWrapper.eq("user_id",complainParam.getUserId());
        if(complainMapper.selectOne(complainQueryWrapper)!=null){
            return Result.success(null);
        }else {
            Complain complain = new Complain();
            complain.setArticleId(complainParam.getArticleId());
            complain.setAuthorId(complainParam.getAuthorId());
            complain.setUserId(complainParam.getUserId());
            complain.setComplainType(complainParam.getComplainType());
            complainMapper.insert(complain);
            return Result.success("举报成功");
        }
    }

    @Override
    public Result selectComplain(ComplainParam complainParam) {
        Page<ComplainVo> complainVoPage=new Page<>(complainParam.getPage(),complainParam.getPageSize());
        IPage<ComplainVo> complainVoIPage=complainMapper.selectComplain(complainVoPage,complainParam.getTitle(),complainParam.getComplainType(),complainParam.getAccount());
        return Result.success(complainVoIPage);
    }

    @Override
    public Result deleteComplain(Long id) {
//        HashMap<String,Object> complainHash=new HashMap<>();
//        complainHash.put("article_id",complainParam.getArticleId());
//        complainHash.put("user_id",complainParam.getUserId());
//        complainMapper.deleteByMap(complainHash);
        complainMapper.deleteById(id);
        return Result.success("删除成功");
    }
}
